﻿using System;
using cz.zcu.PreSti.XNADirectX;

namespace cz.zcu.PreSti.Components
{
    public class OutputComponent : RunnableComponent
    {
        private byte data;
        private XNADirectXPresentation present;

        #region properties

        public byte Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }

        #endregion

        public OutputComponent(IPresentation p)
            : base(p, 1, 0, 1, 0)
        {
            if (p is XNADirectXPresentation)
            {
                present = (XNADirectXPresentation)p;
            }
            else
            {
                throw new InvalidCastException("Cannot perform retyping.");
            }

            Finished += new EventHandler(onFinished);
        }

        public override void update() { }

        public override void trigIn(int n)
        {
            if (present.IsTerminating == false)
            {
                present.startStopClock();
            }
            cz.zcu.PreSti.ParallelPortLibrary.write(Data, 1);
        }

        public override void setValueToSetter(int setterN, object value)
        {
            base.setValueToSetter(setterN, value);
            switch (setterN)
            {
                case 1:
                    int val = 0;
                    convertValue(ref val, value);
                    data = (byte)val;
                    break;
            }
        }

        private void onFinished(object sender, EventArgs e) { }

    }
}
